package com.yunc.modules.sys.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrSpliter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.utils.R;
import com.yunc.modules.sys.entity.SysDictData;
import com.yunc.modules.sys.service.ISysDictDataService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 字典数据
 *
 * @author congcong tao
 * @date 2018/11/19 19:06
 */
@RestController
@RequestMapping("sysDictData")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysDictDataController {

    private final ISysDictDataService sysDictDataService;

    @GetMapping("/page")
    public R page(Page<SysDictData> page, SysDictData sysDictData) {
        QueryWrapper<SysDictData> ew = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(sysDictData.getName())) {
            ew.like("name", sysDictData.getName());
        }
        if (StringUtils.isNotEmpty(sysDictData.getDictType())) {
            ew.eq("dict_type", sysDictData.getDictType());
        }
        ew.orderByAsc("order_num");
        sysDictDataService.page(page, ew);
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/list")
    public R list(SysDictData sysDictData) {
        QueryWrapper<SysDictData> ew = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(sysDictData.getDictType())) {
            ew.eq("dict_type", sysDictData.getDictType());
        }
        ew.orderByAsc("order_num");
        List<SysDictData> list = sysDictDataService.list(ew);
        return R.ok().put("list", list);
    }

    @GetMapping("/dictType/{dictType}")
    public R dictType(@PathVariable String dictType) {
        QueryWrapper<SysDictData> ew = new QueryWrapper<>();
        ew.eq("dict_type", dictType);
        ew.orderByAsc("order_num");
        List<SysDictData> list = sysDictDataService.list(ew);
        return R.ok().put("data", list);
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable String id) {
        SysDictData dictData = sysDictDataService.getById(id);
        return R.ok().put("sysDictData", dictData);
    }

    @GetMapping("/infoBy")
    public R info(SysDictData sysDictData) {
        QueryWrapper<SysDictData> qw = new QueryWrapper<>(sysDictData);
        SysDictData dictData = sysDictDataService.getOne(qw);
        return R.ok().put("sysDictData", dictData);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @CacheEvict(value = {"sys_dict_data_all"}, allEntries = true)
    public R save(SysDictData dictData) {
        dictData.setCreateTime(new Date());
        boolean flag = sysDictDataService.save(dictData);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @PutMapping(value = "/update")
    @CacheEvict(value = {"sys_dict_data_all"}, allEntries = true)
    public R update(SysDictData dictData) {
        boolean flag = sysDictDataService.updateById(dictData);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{ids}")
    @CacheEvict(value = {"sys_dict_data_all"}, allEntries = true)
    public R delete(@PathVariable String ids) {
        List<String> idsList = StrSpliter.splitTrim(ids, ',', true);
        if (CollUtil.isEmpty(idsList)) {
            return R.error("传入值格式错误");
        }
        boolean flag = sysDictDataService.removeByIds(idsList);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
